package com.fzg.service.impl;

import com.aliyun.oss.OSSClient;
import com.fzg.common.enums.ResponseWrapper;
import com.fzg.common.model.param.alioss.DeleteBlogImageOssParam;
import com.fzg.common.model.param.alioss.DeleteImageOssParam;
import com.fzg.entity.Alioss;
import com.fzg.entity.Blog;
import com.fzg.entity.Diary;
import com.fzg.mapper.AliossMapper;
import com.fzg.mapper.BlogMapper;
import com.fzg.mapper.DiaryMapper;
import com.fzg.service.AliossService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author fzg
 * @since 2022-07-01
 */
@Service
public class AliossServiceImpl extends ServiceImpl<AliossMapper, Alioss> implements AliossService {

    @Autowired
    private AliossMapper aliossMapper;

    @Autowired
    private DiaryMapper diaryMapper;

    @Autowired
    private BlogMapper blogMapper;

    /**
     * @name 方法名：getAliossParams
     * @description todo: 获取阿里云连接参数
     *
     * @return  ResponseWrapper
     * @date 2022/12/26 11:49
     * @author fzg
     **/
    @Override
    public ResponseWrapper getAliossParams() {
        return ResponseWrapper.markSuccess(getById(1));
    }

    /**
     * @name 方法名：deleteImgByOss
     * @description todo: 删除图片oss(diary)
     *
     * @param param 参数类：DeleteImageOssParam
     * @return ResponseWrapper
     * @date 2022/12/26 11:50
     * @author fzg
     **/
    @Override
    public ResponseWrapper deleteImgByOss(DeleteImageOssParam param) {
        Alioss alioss = aliossMapper.selectById(1);
        //创建OSSClient实例
        String endpoint = "http://" + alioss.getRegion() + ".aliyuncs.com";
        String accessKeyId = alioss.getAccesskeyid();
        String accessKeySecret = alioss.getAccesskeysecret();
        String bucketName = alioss.getBucket();
        OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);

        // 如果是oss里面的图片，就删除
        if (param.getUrl().contains("myblog-mycaveolae.oss-cn-chengdu.aliyuncs.com")){
            // 根据BucketName,objectName删除文件
            ossClient.deleteObject(bucketName, param.getUrl().replace("https://myblog-mycaveolae.oss-cn-chengdu.aliyuncs.com/",""));
            ossClient.shutdown();
        }


        // 根据diaryAid删除对应的图片URL
        Diary diary = diaryMapper.selectById(param.getDiaryAid());
        String diaryImgUrl = diary.getDiaryImgUrl();
        // 日记肯定有一张及其以上的照片
        int index = diaryImgUrl.indexOf(";");
        if (index == -1){
            // 说明只有一张图片，直接更新diaryURL
            diary.setDiaryImgUrl("");
        }else {
            // 说明有两张图片或者更多
            String[] imageArray = diaryImgUrl.split(";");
            ArrayList<String> diaryImageList = new ArrayList<>();
            for (String url : imageArray) {
                if (!url.equals(param.getUrl())){
                    diaryImageList.add(url);
                }
            }
            if (diaryImageList.size() > 0){
                // 将字符串数组转化成stringBuffer
                StringBuilder stringBuilder = new StringBuilder();
                for (String url : diaryImageList) {
                    stringBuilder.append(url);
                    stringBuilder.append(";");
                }
                // 最后将末尾的“；”去掉
                stringBuilder.deleteCharAt(stringBuilder.length()-1);
                // 更新diary的图片URL
                diary.setDiaryImgUrl(stringBuilder.toString());
            }else {
                // 更新diary的图片URL
                diary.setDiaryImgUrl("");
            }
        }
        diaryMapper.updateById(diary);
        return ResponseWrapper.markSuccess();
    }

    /**
     * @name 方法名：deleteBlogImgByOss
     * @description todo: 删除图片oss(blog)
     *
     * @param param 参数类： DeleteBlogImageOssParam
     * @return ResponseWrapper
     * @date 2022/12/26 11:52
     * @author fzg
     **/
    @Override
    public ResponseWrapper deleteBlogImgByOss(DeleteBlogImageOssParam param) {
        Alioss alioss = aliossMapper.selectById(1);
        //创建OSSClient实例
        String endpoint = "http://" + alioss.getRegion() + ".aliyuncs.com";
        String accessKeyId = alioss.getAccesskeyid();
        String accessKeySecret = alioss.getAccesskeysecret();
        String bucketName = alioss.getBucket();
        OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);

        if (param.getUrl().contains("https://myblog")){
            // 说明这是oss阿里云上面的图片可以正常删掉
            // 根据BucketName,objectName删除文件
            ossClient.deleteObject(bucketName, param.getUrl().replace("https://myblog-mycaveolae.oss-cn-chengdu.aliyuncs.com/",""));
            ossClient.shutdown();
        }else {
            // 这个图片不是oss上的，直接更新数据库就好
            // 这里不用做任何事
        }

        // 根据blogAid删除对应的图片URL
        Blog blog = blogMapper.selectById(param.getBlogAid());
        String blogImgUrl = blog.getBlogImgurl();
        // 博客肯定有一张及其以上的照片
        int index = blogImgUrl.indexOf(";");
        if (index == -1){
            // 说明只有一张图片，直接更新blogURL
            Blog blog1 = new Blog();
            BeanUtils.copyProperties(blog,blog1);
            blog1.setBlogImgurl("");
            System.out.println("fzg1"+blog);
            blogMapper.updateById(blog1);
        }else {
            // 说明有两张图片或者更多
            String[] imageArray = blogImgUrl.split(";");
            ArrayList<String> blogImageList = new ArrayList<>();
            for (String url : imageArray) {
                if (!url.equals(param.getUrl())){
                    blogImageList.add(url);
                }
            }
            // 将字符串数组转化成stringBuffer
            StringBuilder stringBuilder = new StringBuilder();
            for (String url : blogImageList) {
                stringBuilder.append(url);
                stringBuilder.append(";");
            }
            // 最后将末尾的“；”去掉
            stringBuilder.deleteCharAt(stringBuilder.length()-1);
            // 更新blog的图片URL
            blog.setBlogImgurl(stringBuilder.toString());
            blogMapper.updateById(blog);
        }
        return ResponseWrapper.markSuccess();
    }

}
